package com.lanou.beautifulwardrobe.factory;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.Response;
import com.google.gson.Gson;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.activity.BrandShopActivity;
import com.lanou.beautifulwardrobe.activity.IntroFActivity;
import com.lanou.beautifulwardrobe.activity.IntroOActivity;
import com.lanou.beautifulwardrobe.contants.Contants;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.IntroData;
import com.lanou.beautifulwardrobe.entify.IntroResult;

import java.io.Serializable;

/**
 * Created by dllo on 15/10/22.
 */
public class RecommendImageFragment extends Fragment {

    private static RecommendImageFragment recommendImageFragment;
    private String imageUrl;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private String id;
    private ImageView recommendImage;


    public static RecommendImageFragment getInstance(String imageUrl, String id) {
        RecommendImageFragment recommendImageFragment = new RecommendImageFragment();
        Bundle bundle = new Bundle();
        bundle.putString("imageUrl", imageUrl);
        bundle.putString("id", id);
        recommendImageFragment.setArguments(bundle);
        return recommendImageFragment;

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            imageUrl = bundle.getString("imageUrl");
            id = bundle.getString("id");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.recommend_images_item, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recommendImage = (ImageView) view.findViewById(R.id.recommend_image_item);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    public void initView() {
        volleySingleton.getImageResuest(imageUrl, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                recommendImage.setImageBitmap(response);
            }
        });
    }

    private void initData() {
        recommendImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Integer.parseInt(id) < 100000) {
                    Intent intent = new Intent(getActivity(), BrandShopActivity.class);
                    intent.putExtra("id", id + "");
                    getActivity().startActivity(intent);
                } else {
                    String url = Contants.INTRO_URL_PREFIX + id + Contants.INTRO_URL_SUFFIX;
                    volleySingleton.getStringRequset(url, new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            Gson gson = new Gson();
                            IntroResult introResult = gson.fromJson(response, IntroResult.class);
                            IntroData introData = introResult.getData();
                            String activityType=introData.getContent().getActivity_type();
                            if (activityType.equals("5")) {
                                Intent intent = new Intent(getActivity(), IntroFActivity.class);
                                intent.putExtra("data", (Serializable) introData);
                                getActivity().startActivity(intent);
                            } else if(activityType.equals("1")){
                                String productUrl = Contants.INTRO_URL_PRODUCT_PREFIX + id + Contants.INTRO_URL_PRODUCT_SUFFIX;
                                Intent intent = new Intent(getActivity(), IntroOActivity.class);
                                intent.putExtra("data", (Serializable) introData);
                                intent.putExtra("url", productUrl);
                                getActivity().startActivity(intent);
                            }

                        }
                    });
                }
            }
        });

    }
}
